package org.flowable.stress;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;

import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

public class StressMysqlTest {

    // 并发次数
    private final static int CONCURRENT_TIMES = 1000;

    // 线程池设置
    static ExecutorService e = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        Logger.getGlobal().info(
                () -> "并发测试开始。" + "并发次数：" + CONCURRENT_TIMES + "次。\n");

        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://130.51.23.249:3306/activiti6ui?characterEncoding=utf8&useSSL=true")
                .setJdbcUsername("sa")
                .setJdbcPassword("")
                .setJdbcDriver("com.mysql.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                .setJdbcMaxActiveConnections(100)
                ;

        ProcessEngine processEngine = cfg.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.createDeployment()
                .addClasspathResource("StressProcess.bpmn")
                .deploy();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        System.out.println(LocalTime.now() + "并发开始。");

        for (int i = 0; i < CONCURRENT_TIMES; i++) {
//            System.out.println(i);
            e.submit(() -> {
                try {
//                    PrintServiceTest.runProcess();
                    runtimeService.startProcessInstanceByKey("stressProcess");
                    Thread.yield();
                } catch (Exception ex) {
//                    ex.printStackTrace();
                }
            });
        }

        System.out.println(LocalTime.now() + "并发完成");

    }
}
